package com.ruoyi.common.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;

public class ConvertHelper {

    private static final Logger logger = LoggerFactory.getLogger(ConvertHelper.class);

    public static <T> List<T> DataSetToIList(Class<T> tClass, ResultSet resultSet) {
        List<T> result = new ArrayList<T>();
        if (resultSet == null) return result;
        try {
            while (resultSet.next()) {
                T t = tClass.newInstance();
                Field[] fields = tClass.getDeclaredFields();
                for (Field field : fields
                ) {
                    if (isExistColumn(resultSet, field.getName())) {
                        field.set(t, resultSet.getObject(field.getName()));
                    }
                }
                result.add(t);
            }
            return result;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            return result;
        }
    }

    public static List<Hashtable<String, String>> DataSetToHashTable(ResultSet resultSet) {
        List<Hashtable<String, String>> result = new ArrayList<>();
        if (resultSet == null) return result;
        List<String> columNames = getColumNames(resultSet);
        try {
            while (resultSet.next()) {
                Hashtable<String, String> value = new Hashtable<String, String>();
                for (String colunName : columNames
                ) {
                    if (resultSet.getObject(colunName) != null) {
                        value.put(colunName, resultSet.getObject(colunName).toString());
                    }
                }
                result.add(value);
            }
            return result;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            return result;
        }
    }

    public static List<LinkedHashMap<String, String>> DataSetToHashMap(ResultSet resultSet) {
        List<LinkedHashMap<String, String>> result = new ArrayList<>();
        if (resultSet == null) return result;
        List<String> columNames = getColumNames(resultSet);
        try {
            while (resultSet.next()) {
                LinkedHashMap<String, String> value = new LinkedHashMap<>();
                for (String colunName : columNames
                ) {
                    if (resultSet.getObject(colunName) != null) {
                        value.put(colunName, resultSet.getObject(colunName).toString());
                    }
                }
                result.add(value);
            }
            return result;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            return result;
        }
    }

    public static List<String> getColumNames(ResultSet resultSet) {
        List<String> result = new ArrayList<String>();
        try {
            ResultSetMetaData metaData = resultSet.getMetaData();
            Integer columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                result.add(metaData.getColumnName(i));
            }
            return result;
        } catch (Exception ex) {
            return result;
        }
    }

    public static boolean isExistColumn(ResultSet rs, String columnName) {
        try {
            if (rs.findColumn(columnName) > 0) {
                return true;
            }
        } catch (SQLException ex) {
            return false;
        }

        return false;
    }

    public static String toSqlString(String value) {
        if (value == null || value.isEmpty()) return null;
        return "'" + value + "'";
    }

}
